﻿@model DBMS.Models.RelationshipViewModel

@{
    ViewBag.Title = "SetForeign";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>SetForeign</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<script>
    $(document).ready(function () {
        FillDropdown();

        $("#PrimaryTable").change(function () {
            FillDropdown();
        });
    });

    function FillDropdown() {
        var databaseId = "@ViewBag.DatabaseId";
        var tableId = $("#PrimaryTable").val();

        $.getJSON("/Columns/GetAllColumnsForTable?databaseId=" + databaseId + "&tableId=" + tableId,
            function (data) {
                $("#dropDown").html("");
                var s = $('<select id="PrimaryColumn" name="PrimaryColumn"/>');
                $.each(data, function (i, item) {
                    $('<option />', { value: item.Index, text: item.Name }).appendTo(s);
                });
                s.appendTo($("#dropDown"));
            });
    }
</script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    
    @Html.Hidden("ForeignTable", Model.ForeignTable.Index)
    @Html.Hidden("ForeignColumn", Model.ForeignColumn.Index)
    
    <fieldset>
        <legend>RelationshipViewModel</legend>

        <table>
            <thead>
                <tr>
                    <td></td>
                    <td>Primary</td>
                    <td>Foreign</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Table</td>
                    <td>@Html.DropDownListFor(model => model.PrimaryTable, Model.Tables)</td>
                    <td>@Model.ForeignTable.Name</td>
                </tr>
                <tr>
                    <td>Column</td>
                    <td id="dropDown"></td>
                    <td>@Model.ForeignColumn.Name</td>
                </tr>
            </tbody>
        </table>
        

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
